package com.yc.controller;

import com.yc.model.Address;
import com.yc.service.AddressService;
import com.yc.web.bean.AddressDTO;
import com.yc.web.bean.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/address")
public class AddressController {
    @Autowired
    private AddressService addressService;


    /**
     * 添加地址
     * @param userId
     * @param addressDTO
     * @return
     */
    @PostMapping("/add")
    public ResponseEntity<ResponseResult> addAddress(@RequestHeader("X-User-ID") Long userId,
                                                     @RequestBody AddressDTO addressDTO){ //前端传来的数据
        try {
            AddressDTO addressList = addressService.addAddress(userId,addressDTO);

            return new ResponseEntity<>(ResponseResult.ok( "添加地址成功").setData( addressList), HttpStatus.OK);
        } catch (IllegalArgumentException e) {
            return new ResponseEntity<>(ResponseResult.error(e.getMessage()), HttpStatus.BAD_REQUEST);
        } catch (Exception e) {
            return new ResponseEntity<>(ResponseResult.error("添加地址失败：" + e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 查看地址列表
     * @param userId
     * @return
     */
    @RequestMapping("/list")
    public ResponseEntity<ResponseResult> listAddress(@RequestHeader("X-User-ID") Long userId){
        List<AddressDTO> addressList = addressService.listAddress(userId);
        return new ResponseEntity<>(ResponseResult.ok( "获取地址列表成功").setData( addressList), HttpStatus.OK);
    }
    /**
     * 更新地址信息
     * PUT /back/address/{addressId}
     *
     * @param addressId 地址ID
     * @param userId 从请求头获取用户ID
     * @param addressDTO 地址数据
     * @return 更新后的地址信息
     */
    @PutMapping("/update/{addressId}")
    public ResponseEntity<ResponseResult> updateAddress(@PathVariable("addressId") Long addressId,
                                                                    @RequestHeader("X-User-ID") Long userId,
                                                                    @RequestBody AddressDTO addressDTO) {
        try {
            addressService.updateAddress(addressId, userId, addressDTO);
            return new ResponseEntity<>(ResponseResult.ok("更新地址成功"), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(ResponseResult.error("更新地址失败：" + e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
    /**
     * 软删除地址
     * DELETE /back/address/{addressId}
     *
     * @param addressId 地址ID
     * @return 成功信息
     */
    @DeleteMapping("/delete/{addressId}")
    public ResponseEntity<ResponseResult> deleteAddress(@PathVariable("addressId") Long addressId) {
        addressService.softDeleteAddress(addressId);
        return new ResponseEntity<>(ResponseResult.ok("删除地址成功"), HttpStatus.OK);
    }

    /**
     * 设置某个地址为默认地址
     * PUT /back/orders/address/default/{addressId}
     *
     * @param addressId 地址ID
     * @param userId 从请求头获取用户ID
     * @return 成功信息
     */
    @PutMapping("/default/{addressId}")
    public ResponseEntity<ResponseResult> setDefaultAddress(@PathVariable("addressId") Long addressId, @RequestHeader("X-User-ID") Long userId) {
        addressService.setDefaultAddress(userId, addressId);
        return new ResponseEntity<>(ResponseResult.ok("设置默认地址成功"), HttpStatus.OK);
    }

}
